package com.mall.malladminbackend.controller;

import com.mall.malladminbackend.dto.InsertMenuDto;
import com.mall.malladminbackend.dto.MenusDto;
import com.mall.malladminbackend.model.R;
import com.mall.malladminbackend.service.MenuService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

@RestController
@RequestMapping("/menu")
@AllArgsConstructor
public class MenuController {

    private MenuService menuService;

    /**
     * 如果 Spring Security 的上下文对象中持有 UsernamePasswordAuthenticationToken，
     * 就可以在Controller中通过 Principal 获取到用户名
     * @param principal
     * @return
     */
    @GetMapping
    public R getMenus(Principal principal) {
        String name = principal.getName(); //获取到用户名
        return menuService.getMenuModels(name);
    }
    @GetMapping("/multistageMenus")
    public R getMultistageMenus(){
        return menuService.getMenuTree();
    }
    @DeleteMapping("/{id}")
    public R deleteMultistageMenus(@PathVariable Integer id){
        return menuService.deleteMenus(id);
    }
    @GetMapping("/{id}")
    public R getAllMenus(@PathVariable Integer id){
        return menuService.selectMenus(id);
    }
    @PutMapping("/getMenus")
    public R updateMenus(@RequestBody MenusDto menusDto) throws Exception {
        return menuService.updateMenus(menusDto);
    }
    //(无用)
    @GetMapping("/parent/{id}")
    public R getAllMenusParentName(@PathVariable Integer id){
        return menuService.queryAllParentName(id);
    }

    @GetMapping("/parents/{type}")
    public R getParentName(@PathVariable Integer type){
        return menuService.getParentName(type);
    }
    @PostMapping
    public R insertMenus(@RequestBody InsertMenuDto insertMenuDto) throws Exception{
        return menuService.insertMenus(insertMenuDto);
    }
}
